//
//  HRDynamicViewModel.m
//  Hear
//
//  Created by mac on 2017/3/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDynamicViewModel.h"
#import "HRDynamicModel.h"
#import "HRDunamicLayout.h"
#import "HRUpdataUserInfoModel.h"
#import "Track.h"
#import "MTMusicFeature.h"
@implementation HRDynamicViewModel

-(void)getDynamicRequestModel:(HRDynamicRequestModel*)requstModel contentArr:(void(^)(NSMutableArray *contentArr))contentArr{
    
    YTKApi *api = [YTKApi apiGetWithUrl:@"MomentsInfo/Get_GetMoments" parameter:requstModel];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            [[NSUserDefaults standardUserDefaults] setInteger:300 forKey:@"cacheTime"];

            NSDictionary *dict = request.responseJSONObject;
            
            NSArray *resultArr = [NSArray modelArrayWithClass:[HRDynamicModel class] json:dict[@"BodyContent"]];
            
            NSMutableArray *layouts = [NSMutableArray array];
            for (HRDynamicModel *model in resultArr) {
                if([model.MomentType intValue] == 2){
                    model.pics = [model.MomentDetai jsonValueDecoded];
                }else if ([model.MomentType intValue] == 5){
                    model.soundAndImageConentModel = [HRDynamicModel modelWithJSON:model.MomentDetai];
                    model.soundAndImageConentModel.pics = [model.soundAndImageConentModel.PhotoUrl jsonValueDecoded];
                    
                    Track *track = [[Track alloc] init];
                    track.audioFileURL = [NSURL URLWithString:model.soundAndImageConentModel.MomentDetai];
                    model.soundAndImageConentModel.musicFeature = [[MTMusicFeature alloc] initMusicFeatureWithAudioFile:track];
                }else if ( [model.MomentType intValue] == 1){
                    Track *track = [[Track alloc] init];
                    track.audioFileURL = [NSURL URLWithString:model.MomentDetai];
                    model.musicFeature = [[MTMusicFeature alloc] initMusicFeatureWithAudioFile:track];
                }else if ([model.MomentType intValue] == 6){
                    model.MomentDetaiModel = [HRDynamicModel modelWithJSON:model.MomentDetai];
                }
                HRDunamicLayout *layout = [[HRDunamicLayout alloc] initWithStatus:model style:0];
                [layouts addObject:layout];
                
            }

            
            contentArr(layouts);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        }];
    }];
}

-(void)postSaveBlackList:(NSDictionary *)requstDict isSeccess:(void(^)(BOOL isSeccess))isSeccess{
    YTKApi *api = [YTKApi apiPostWithUrl:@"UserInfo/Post_SaveBlacklist" para:requstDict];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            NSDictionary *dict = request.responseJSONObject;

            isSeccess(YES);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            isSeccess(NO);
        }];
    }];
}

#pragma mark 添加好友
-(void)saveUserFollow:(HRSaveUserFollowRequestModel *)requstModel isSeccess:(void(^)(BOOL isSeccess))isSeccess{
    YTKApi *api = [YTKApi apiPostWithUrl:@"UserInfo/Post_SaveUserFollow" parameter:requstModel];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithSuccess:^(YTKBaseRequest *request) {
            NSDictionary *dict = request.responseJSONObject;
            isSeccess(YES);
        }];
    }];
}

#pragma mark 删除好友
-(void)deleteUserFollow:(NSDictionary *)requstDict isSeccess:(void(^)(BOOL isSeccess))isSeccess{
    YTKApi *api = [YTKApi apiDeleteWithUrl:@"UserInfo/Delete_DeleteUserFollowInfo" para:requstDict];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            NSDictionary *dict = request.responseJSONObject;
            isSeccess(YES);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            isSeccess(NO);
        }];
    }];
}

#pragma mark 添加关注
-(void)followUserFollow:(HRSaveUserFollowRequestModel *)requstModel isSeccess:(void(^)(BOOL isSeccess))isSeccess {
    YTKApi *api = [YTKApi apiPostWithUrl:@"UserInfo/Post_SaveUserFollow_" parameter:requstModel];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithSuccess:^(YTKBaseRequest *request) {
            NSDictionary *dict = request.responseJSONObject;
            isSeccess(YES);
        }];
    }];
}

#pragma mark 取消关注
-(void)cancelUserFollow:(NSDictionary *)requstDict isSeccess:(void(^)(BOOL isSeccess))isSeccess {
    YTKApi *api = [YTKApi apiDeleteWithUrl:@"UserInfo/Delete_DeleteUserFollowInfo_" para:requstDict];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            NSDictionary *dict = request.responseJSONObject;
            isSeccess(YES);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            isSeccess(NO);
        }];
    }];
}

-(void)getMySoundRequestDict:(NSMutableDictionary*)requestDict contentArr:(void(^)(NSMutableArray *contentArr))contentArr{
    YTKApi *api = [YTKApi apiGetWithUrl:@"SoundInfo/Get_GetMySound" para:requestDict];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            NSDictionary *dict = request.responseJSONObject;
            NSArray *resultArr = [NSArray modelArrayWithClass:[HRDynamicModel class] json:dict[@"BodyContent"]];

            NSMutableArray *layouts = [NSMutableArray array];
            for (HRDynamicModel *model in resultArr) {
                if([model.MomentType intValue] == 2){
                    model.pics = [model.MomentDetai jsonValueDecoded];
                }else if ([model.MomentType intValue] == 5){
                    model.soundAndImageConentModel = [HRDynamicModel modelWithJSON:model.MomentDetai];
                    model.soundAndImageConentModel.pics = [model.soundAndImageConentModel.PhotoUrl jsonValueDecoded];
                    
                    Track *track = [[Track alloc] init];
                    track.audioFileURL = [NSURL URLWithString:model.soundAndImageConentModel.MomentDetai];
                    model.soundAndImageConentModel.musicFeature = [[MTMusicFeature alloc] initMusicFeatureWithAudioFile:track];
                }else if ( [model.MomentType intValue] == 1){
                    Track *track = [[Track alloc] init];
                    track.audioFileURL = [NSURL URLWithString:model.MomentDetai];
                    model.musicFeature = [[MTMusicFeature alloc] initMusicFeatureWithAudioFile:track];
                }else if ([model.MomentType intValue] == 6){
                    model.MomentDetaiModel = [HRDynamicModel modelWithJSON:model.MomentDetai];
                }
                HRDunamicLayout *layout = [[HRDunamicLayout alloc] initWithStatus:model style:0];
                [layouts addObject:layout];
                
            }
            contentArr(layouts);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            contentArr(nil);
        }];
    }];
}



@end
